package com.tinusgraglin.enclusiv.datalayer.repos

import android.util.Log
import com.tinusgraglin.enclusiv.datalayer.BasicUserProfile
import com.tinusgraglin.enclusiv.datalayer.UserArticleStats
import com.tinusgraglin.enclusiv.datalayer.datasources.UserDataStatsRemoteSource
import com.tinusgraglin.enclusiv.datalayer.datasources.UserRegistrationRemoteSource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class UserRepo (
    private val registrationRemoteSource: UserRegistrationRemoteSource,
    private val userDataStatsRemoteSource: UserDataStatsRemoteSource,
) {
    companion object {
        private var repo: UserRepo? = null

        fun initialize (registrationRemoteSource: UserRegistrationRemoteSource, userDataStatsRemoteSource: UserDataStatsRemoteSource) {
            repo = UserRepo(registrationRemoteSource, userDataStatsRemoteSource)
        }

        fun get() : UserRepo {
            if (repo != null)
                return repo as UserRepo
            else
                throw IllegalStateException("Uninitialized")
        }
    }

    var cachedLoggedInUserProfile: BasicUserProfile? = null
        private set

    suspend fun sendVerificationCodeToEmail(email: String) {
        withContext(Dispatchers.IO) {
            registrationRemoteSource.sendVerificationCodeToEmail(email)
        }
    }

    suspend fun checkVerificationCode(email: String, code: String) : Boolean {
        return withContext(Dispatchers.IO) {
            registrationRemoteSource.checkVerificationCode(email, code)
        }
    }

    suspend fun getUser(email: String, password: String): BasicUserProfile {
        val profile = withContext(Dispatchers.IO) {
            userDataStatsRemoteSource.getUser(email, password)
        }
        cachedLoggedInUserProfile = profile
        Log.d("## DBG 9898", "profile, id ${cachedLoggedInUserProfile!!.id}")
        return profile
    }

    suspend fun register(email: String, password: String) : Long {
        return withContext(Dispatchers.IO) {
            registrationRemoteSource.register(email, password)
        }
    }

    suspend fun getUserArticleStats(uid: Long, aid: Long): UserArticleStats {
        return withContext(Dispatchers.IO) {
            userDataStatsRemoteSource.userArticleStats(uid, aid)
        }
    }

    suspend fun getRecommended(uid: Long) : List<Long> {
        return withContext(Dispatchers.IO) {
            userDataStatsRemoteSource.fromUserGetArticles(uid,
                UserDataStatsRemoteSource.UserArticleRelation.RECOMMENDATION
            )
        }
    }

    suspend fun getOwnedArticle(uid: Long):  List<Long> {
        return withContext(Dispatchers.IO) {
            userDataStatsRemoteSource.fromUserGetArticles(uid,
                UserDataStatsRemoteSource.UserArticleRelation.OWN
            )
        }
    }

    suspend fun like(uid: Long, aid: Long, undo: Boolean) = withContext(Dispatchers.IO) {
        userDataStatsRemoteSource.doUserArticleAction(uid, aid,
            UserDataStatsRemoteSource.UserArticleRelation.LIKE, undo)
    }
    suspend fun fav(uid: Long, aid: Long, undo: Boolean) = withContext(Dispatchers.IO) {
        userDataStatsRemoteSource.doUserArticleAction(uid, aid,
            UserDataStatsRemoteSource.UserArticleRelation.FAV, undo)
    }
}